package com.onlyphoto.onlymall.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.onlyphoto.onlymall.R;
import com.onlyphoto.onlymall.bean.response.RefundReasonBean;
import com.onlyphoto.onlymall.mvp.view.adapter.OptionAdapter;

import java.util.ArrayList;


/**
 * 选项列表选择器
 *
 * Created by gaochujia on 2019-08-27.
 */
public class OptionPopupView extends PopupWindow implements View.OnClickListener {

    private View mPopView;
    private ImageView close;
    private RecyclerView rvOption;
    private LinearLayoutManager layoutManager;
    private OptionAdapter adapter;

    private Activity mActivity;
    private ArrayList<RefundReasonBean> options;

    //地址选择完成的监听
    private OnSelectFinishedListener mListener;

    public OptionPopupView(Context mContext, ArrayList<RefundReasonBean> options) {
        super(mContext);
        mActivity = (Activity) mContext;
        this.options = options;
        init(mContext);
        setPopupWindow();
    }

    //初始化
    private void init(Context context) {
        mPopView = LayoutInflater.from(context).inflate(R.layout.option_select, null);
        close = mPopView.findViewById(R.id.close);
        rvOption = mPopView.findViewById(R.id.rv_option);
        layoutManager = new LinearLayoutManager(context);
        adapter = new OptionAdapter();
        rvOption.setAdapter(adapter);
        adapter.setItemData(options);
        rvOption.setLayoutManager(layoutManager);
    }

    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View

        // 点击其他区域关闭
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setAnimationStyle(R.style.anim_bottom);

        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        Window window = mActivity.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();

        //退出popupWindow时显示父控件原来的颜色
        this.setOnDismissListener(() -> {
            lp.alpha = 1.0f;
            window.setAttributes(lp);
        });
        close.setOnClickListener(this);

        adapter.setOnItemClickListener((id, name) -> {
            dismiss();
            mListener.finished(id, name);
        });
    }

    @Override
    public void onClick(View v) {
        this.dismiss();
    }

    public void setFinishedListener(OnSelectFinishedListener listener){
        this.mListener = listener;
    }

    public interface OnSelectFinishedListener{
        void finished(int id, String name);
    }
}
